package com.uzai.console.common.utils;

import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.dto.user.wechatuser.TimeVO;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * @Auther: liuqi
 * @Date: 2021/08/13 11:26
 * @Description:
 */
public class DateUtil extends cn.hutool.core.date.DateUtil {

    private static SimpleDateFormat tbkFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat jdFormat = new SimpleDateFormat("yyyyMMddHH");
    private static int tbkUnitS = 1200;             // 单位时间(20分钟)1200s
    private static int jdUnitS = 1200;             // 单位时间(20分钟)1200s
    private static int pddUnits = 3600;            // 单位时间(60分钟)3600s
    private static int tbRefundUnitS = 1200;       //单位时间(20分钟)1200s

    public static int getNowTime(){
        return getTime(new Date());
    }

    public static int getTime(Date date){
        if (date==null){
            return 0;
        }
        return (int) (date.getTime()/1000);
    }

    public static int getStrTime(String date){
        return getTime(DateFormatUtil.StringToDateFormat(date));
    }
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取时间所在月第一天和最后一天
     * @param start_time
     * @param num 0:第一天  1:最后一天
     * @return
     */
    public static String getEndDayOrOneDay(Date start_time, int num){
        String time = "";
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if(num==0){
            // 获取当前月的第一天
            cale = Calendar.getInstance();
            cale.setTime(start_time);
            cale.add(Calendar.MONTH, 0);
            cale.set(Calendar.DAY_OF_MONTH, 1);
            time = format.format(cale.getTime());
        }else{
            // 获取当前月的最后一天
            cale = Calendar.getInstance();
            cale.setTime(start_time);
            cale.add(Calendar.MONTH, 1);
            cale.set(Calendar.DAY_OF_MONTH, 0);
            time = format.format(cale.getTime());
        }
        //return Integer.parseInt(time);
        return time;
    }

    /**
     * 获取时间指定月的开始时间戳或者结束时间戳
     * @param month(格式：yyyy-MM)
     * @param num 0:开始时间戳  1:结束时间戳
     * @return
     */
    public static Integer getStartTimeOrEndTimeByMonth(String month, int num){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String startMonth = getEndDayOrOneDay(sdf.parse(month),0);
            String endMonth = getEndDayOrOneDay(sdf.parse(month),1);
            if(num==0){
                startMonth = startMonth + " 00:00:00";
                Date startMonthDate =   DateFormatUtil.StringToDateFormat(startMonth);
                return DateUtil.getTime(startMonthDate);
            }else{
                endMonth = endMonth + " 23:59:59";
                Date endMonthDate =   DateFormatUtil.StringToDateFormat(endMonth);
                return DateUtil.getTime(endMonthDate);
            }
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取指定时间的时间戳
     * @param localDateTime
     * @return
     */
    public static Long getLocalDateTimeStamp(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.UTC);
    }

    /**
     * 返回起始结束时间查询的时间范围集合(unit为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @param unit              间隔时间 (s)
     * @return
     */
    public static List<TimeVO> getTimeScope(Integer begin_time, Integer end_time, Integer unit){
        List<TimeVO> list = new ArrayList<>();

        if (begin_time > end_time){
            return list;
        }

        int scopeCount = (end_time - begin_time) / unit;

        if (scopeCount==0){
            TimeVO timeVO = new TimeVO();
            timeVO.setBeginTime(begin_time);
            timeVO.setEndTime(end_time);
            list.add(timeVO);
        }else {
            Integer tempBeginTime = begin_time;
            for (int i=1; i<=scopeCount; i++){
                Integer tempEndTime = tempBeginTime + unit;
                TimeVO timeVO = new TimeVO();
                timeVO.setBeginTime(tempBeginTime);
                timeVO.setEndTime(tempEndTime);
                list.add(timeVO);
                tempBeginTime = tempEndTime + 1;
            }

            int smallTime = (end_time - begin_time) % unit;
            if (smallTime > 0){
                TimeVO timeVO = new TimeVO();
                timeVO.setBeginTime(tempBeginTime);
                timeVO.setEndTime(end_time);
                list.add(timeVO);
            }
        }
        return list;
    }

    /**
     * 获取淘宝订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return
     */
    public static List<TimeVO> getTbkOrderTimeScope(Integer begin_time, Integer end_time){
        return getTbkOrderTimeVOS(begin_time, end_time, tbkUnitS);
    }

    private static List<TimeVO> getTbkOrderTimeVOS(Integer begin_time, Integer end_time, int tbkUnitS) {
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat tbkFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<TimeVO> list = new ArrayList<>();
        int scopeCount = (end_time-begin_time) / tbkUnitS;
        if (scopeCount==0){
            TimeVO timeVO = new TimeVO();
            timeVO.setBeginTime(begin_time);
            timeVO.setEndTime(end_time);
            list.add(timeVO);
        }else{
            Integer tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Integer tempEndTime = tempBeginTime+ tbkUnitS;
                TimeVO timeVO = new TimeVO();
                timeVO.setBeginTime(tempBeginTime);
                timeVO.setEndTime(tempEndTime);
                list.add(timeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (end_time-begin_time)% tbkUnitS;
            if (smallTime>0){
                TimeVO timeVO = new TimeVO();
                timeVO.setBeginTime(tempBeginTime);
                timeVO.setEndTime(end_time);
                list.add(timeVO);
            }
        }
        return list;
    }

    /**
     * 获取淘宝维权订单查询的时间范围集合(12小时为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return
     */
    public static List<TimeVO> getTbkOrderRefundTimeScope(Integer begin_time, Integer end_time){
        return getTbkOrderTimeVOS(begin_time, end_time, tbRefundUnitS);
    }


    /**
     * 获取京东订单查询的时间范围集合(60分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return List<JdOrderTimeVO>
     */
    public static List<TimeVO> getJdOrderTimeScope_ByAppKey(Integer begin_time, Integer end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat tbkFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<TimeVO> list = new ArrayList<>();
        int scopeCount = (end_time-begin_time)/ jdUnitS;
        if (scopeCount==0){
            TimeVO timeVO = new TimeVO();
            timeVO.setBeginTime(begin_time);
            timeVO.setEndTime(end_time);
            list.add(timeVO);
        }else {
            Integer tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Integer tempEndTime = tempBeginTime + jdUnitS;
                TimeVO timeVO = new TimeVO();
                timeVO.setBeginTime(tempBeginTime);
                timeVO.setEndTime(tempEndTime);
                list.add(timeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (end_time-begin_time)% jdUnitS;
            if (smallTime>0){
                TimeVO timeVO = new TimeVO();
                timeVO.setBeginTime(tempBeginTime);
                timeVO.setEndTime(end_time);
                list.add(timeVO);
            }
        }
        return list;
    }




    public static void main(String[] args){

        System.out.println(getLocalDateTimeStamp(LocalDateTime.of(2024, 6, 1, 0, 0)));
        LocalDate now = LocalDate.of(2024,5,31);
        System.out.println(now.isAfter(LocalDate.of(2024, 5, 31)));
        String yyyyMMdd = Tools.getDateFormat(new Date(), "yyyy-MM-dd");

        //开始限制时间（HH:mm）
        String limitTimeStart = "23:00";

        limitTimeStart = yyyyMMdd + " " + limitTimeStart + ":00";

        //结束限制时间（HH:mm）
        String limitTimeEnd = "22:59";
        limitTimeEnd = yyyyMMdd + " " + limitTimeEnd + ":59";

        int limitTimeStartInt = DateUtil.getStrTime(limitTimeStart);
        int limitTimeEndInt = DateUtil.getStrTime(limitTimeEnd);

        //如果开始时间小于结束时间，则结束时间取第二天的这个时间
        if(limitTimeStartInt > limitTimeEndInt){
            String limitTimeEnd_aften = Tools.getSpecifiedDayBefore(limitTimeEnd, "yyyy-MM-dd HH:mm:ss", -1);
            limitTimeEndInt = DateUtil.getStrTime(limitTimeEnd_aften);
        }

        System.out.println(limitTimeStartInt);

        System.out.println(limitTimeEndInt);

    }

}
